/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beanControl;

import Utils.Helper;
import Utils.JDBCHelper;
import Utils.StringUtility;
import beanInfo.interest;
import beanInfo.loanProfile;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JTable;

/**
 *
 * @author lyan
 */
public class loanProfileControl {

    ArrayList<String> listColumn = new ArrayList<String>();
    ArrayList<String> listValue = new ArrayList<String>();
    loanProfile loan = new loanProfile();

    public void getlistLoanProfile(JTable table) {

        String sql = "select ProfileID,InterestID,CusID,UserName,TotalCapital,"
                + "LoanTime,LoanTerm,PrincipalAmount,InterestPayable,Status "
                + "from LoanProfile";

        String[] header = {"ProfileID", "InterestID", "CusID", "UserName",
            "TotalCapital", "LoanTime", "LoanTerm", "PrincipalAmount",
            "InterestPayable", "Status"};

        Helper.loadDataTable(sql, table, header);

    }

    private int checkDelteloanProfile(String values) {
        return JDBCHelper.excuteQueryReturnInt("ProfileID", "PayOriginal ", values);
    }

    public void DeleteLoanProfile(loanProfile loan) {
        if (checkDelteloanProfile(String.valueOf(loan.getProfileID())) > 0) {
            Helper.inforMessage("Can not detlete this loanProfile", "Wanning");
        } else {
            JDBCHelper.excuteDelete("ProfileID", "LoanProfile", String.valueOf(loan.getProfileID()));
            Helper.successMessage("Delete successfull", "wanning");
        }

    }

    public int getMaxLoanprofiletID() {
        int id = 0;
        try {
            String sql = "select max (CusID) from Customers";
            ResultSet rs = JDBCHelper.excuteSelect(sql);
            if (rs.next()) {
                id = rs.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(loanProfileControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    public void getListInterestCBO(JComboBox cbo) {
        String sql = "select * from interest";
        Helper.getListCBO(sql, cbo, "LimitInterset", "InterestID");

    }

    public void insertLoanProfile(loanProfile lp) {
        Object[] values = {lp.getInterestID(), lp.getCusID(),
            lp.getUserName(), lp.getTotalAmount(), lp.getDateLoan(),
            lp.getLimitInterset(), lp.getStatus()};
        JDBCHelper.excuteInsertPlaceHolder("LoanProfile", 7, values);
        
    }
    
    public void getListBrancheCBO(JComboBox cbo)
    {
        String sql = "select * from Branches";
        Helper.getListCBO(sql, cbo, "BranchName", "BranchesID");
    }
    
    
    public interest getInterestDetail(String id)
    {
        interest it = new interest();
        try {
            
            String sql = "select * from interest where InterestID = '"+id+"'";
            System.out.println(sql);
            ResultSet rs = JDBCHelper.excuteSelect(sql);
            while(rs.next())
            {
                it.setInterestID(rs.getInt(""));
                it.setLimitInterset(rs.getFloat("LimitInterset"));
                it.setUnLimitInterest(rs.getFloat("UnLimitInterest"));
                it.setYears(rs.getInt("Years"));
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(loanProfileControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return it;
    }
    public int getMaxZCusID() {
        int id = 0;
        try {
            String sql = "select max (CusID) from Customers";
            ResultSet rs = JDBCHelper.excuteSelect(sql);
            if (rs.next()) {
                id = rs.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(purposesControl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }
}
